package com.heihei.serviceimpl.usr;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heihei.common.page.Condition;
import com.heihei.common.page.Query;
import com.heihei.common.response.ResponseVO;
import com.heihei.entity.usr.UsrRoleEntity;
import com.heihei.enums.DeleteFlagEnum;
import com.heihei.mapper.usr.UsrRoleMapper;
import com.heihei.service.usr.UsrRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户认证 - 角色表 服务实现类
 * </p>
 *
 * @author duanhengfei
 * @since 2021-11-30
 */
@Service
public class UsrRoleServiceImpl extends ServiceImpl<UsrRoleMapper, UsrRoleEntity> implements UsrRoleService {

    /**
     * 添加
     *
     * @author duanhengfei
     * @date 2021年11月30日 23:46:23
     */
    @Override
    public ResponseVO<String> myAdd(UsrRoleEntity paramEntity) {
        //校验
        if(StringUtils.isEmpty(paramEntity.getRoleCode())) {
            return ResponseVO.paramLess("角色编码不能为空");
        }
        if(StringUtils.isEmpty(paramEntity.getRoleName())) {
            return ResponseVO.paramLess("角色名称不能为空");
        }

        paramEntity.setId(null);
        LambdaQueryWrapper<UsrRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UsrRoleEntity::getDelFlag, DeleteFlagEnum.NOT_DELETED.getKey())
                .eq(UsrRoleEntity::getRoleCode, paramEntity.getRoleCode());
        List<UsrRoleEntity> list = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(list)) {
            return ResponseVO.paramIllegal("已有重复的角色编码");
        }

        boolean flag = this.save(paramEntity);
        if(!flag) {
            return ResponseVO.dealFail("添加失败");
        }
        return ResponseVO.dealSuccess(paramEntity.getId());
    }

    /**
     * 修改
     *
     * @author duanhengfei
     * @date 2021年11月30日 23:49:08
     */
    @Override
    public ResponseVO<Boolean> myUpdateById(UsrRoleEntity paramEntity) {
        if(StringUtils.isEmpty(paramEntity.getId())) {
            return ResponseVO.paramLess("请传入主键ID");
        }
        // update 表名 where delFlag = DeleteFlagEnum.NOT_DELETED.getKey() and id =#{paramEntity.id}
        UpdateWrapper<UsrRoleEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(UsrRoleEntity::getDelFlag, DeleteFlagEnum.NOT_DELETED.getKey())
                .eq(UsrRoleEntity::getId, paramEntity.getId());
        boolean flag = this.update(paramEntity, updateWrapper);
//        boolean flag = this.updateById(paramEntity);
        if(!flag) {
            return ResponseVO.paramIllegal("可能不存在该记录");
        }
        return ResponseVO.dealSuccess(true);
    }

    /**
     * 删除
     *
     * @author duanhengfei
     * @date 2021年11月30日 23:55:09
     */
    @Override
    public ResponseVO<Boolean> deleteByIdLogic(String id) {
        if(StringUtils.isEmpty(id)) {
            return ResponseVO.paramLess("请传入主键ID");
        }

        UsrRoleEntity entity = new UsrRoleEntity();
        entity.setId(id);
        entity.setDelFlag(DeleteFlagEnum.DELETED.getKey());

        boolean flag = this.updateById(entity);
        if(!flag) {
            return ResponseVO.paramIllegal("可能不存在该记录");
        }
        return ResponseVO.dealSuccess(true);
    }

    /**
     * 批量删除
     *
     * @author duanhengfei
     * @date 2021年11月30日 23:58:02
     */
    @Override
    public ResponseVO<Boolean> deleteBatch(List<String> idList) {
        if(CollectionUtils.isEmpty(idList)) {
            return ResponseVO.paramLess("请传入主键ID");
        }

        List<UsrRoleEntity> entityList = idList.stream().map(id -> {
            UsrRoleEntity entity = new UsrRoleEntity();
            entity.setId(id);
            entity.setDelFlag(DeleteFlagEnum.DELETED.getKey());
            return entity;
        }).collect(Collectors.toList());

        boolean flag = this.updateBatchById(entityList);
        if(!flag) {
            return ResponseVO.paramIllegal("可能不存在该记录");
        }
        return ResponseVO.dealSuccess(true);
    }

    /**
     * 列表查询（分页）
     *
     * @author duanhengfei
     * @date 2021年12月01日 00:01:45
     */
    @Override
    public ResponseVO<IPage<UsrRoleEntity>> myPageList(UsrRoleEntity entityParam, Query query) {
        LambdaQueryWrapper<UsrRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        //只查询未删除的数据
        queryWrapper.eq(UsrRoleEntity::getDelFlag, DeleteFlagEnum.NOT_DELETED.getKey());

        if(StringUtils.isNotEmpty(entityParam.getRoleName())) {
            queryWrapper.like(UsrRoleEntity::getRoleName, entityParam.getRoleName());
        }
        if(StringUtils.isNotEmpty(entityParam.getRoleCode())) {
            queryWrapper.eq(UsrRoleEntity::getRoleCode, entityParam.getRoleCode());
        }
        IPage<UsrRoleEntity> pageData = this.page(Condition.getPage(query), queryWrapper);
        return ResponseVO.dealSuccess(pageData);
    }

    /**
     * 详情
     *
     * @author duanhengfei
     * @date 2021年12月01日 00:06:25
     */
    @Override
    public ResponseVO<UsrRoleEntity> myGetById(String id) {
        if(StringUtils.isEmpty(id)) {
            return ResponseVO.paramLess("请传入主键ID");
        }
        UsrRoleEntity entity = this.getById(id);
        if(entity == null || DeleteFlagEnum.DELETED.getKey().equals(entity.getDelFlag())) {
            return ResponseVO.paramIllegal("该记录不存在，或者已被删除");
        }
        return ResponseVO.dealSuccess(entity);
    }
}